<template>
  <a-form :form="form">
    <div v-for="(item, index) in empTrain.empTrainList" :key="item.guid || item.id">
      <a-col :span="10">
        <a-form-item label="培训名称" :label-col="{ span: 9 }" :wrapper-col="{ span: 15 }">
          <span class="text_color" v-if="disable">{{ item.trainCourse }}</span>
          <a-input
            v-decorator="[`trainCourse-${index}`, { initialValue: item.trainCourse }]"
            v-else
            @change="change($event, index, 'trainCourse')"
          />
        </a-form-item>
      </a-col>
      <a-col :span="10">
        <a-form-item label="培训单位" :label-col="{ span: 9 }" :wrapper-col="{ span: 15 }">
          <span class="text_color" v-if="disable">{{ item.trainOrgName }}</span>
          <a-input
            v-decorator="[`trainOrgName-${index}`, { initialValue: item.trainOrgName }]"
            v-else
            @change="change($event, index, 'trainOrgName')"
          />
        </a-form-item>
      </a-col>
      <a-col :span="10">
        <a-form-item label="培训开始时间" :label-col="{ span: 9 }" :wrapper-col="{ span: 15 }">
          <span class="text_color" v-if="disable">{{ item.startTime }}</span>
          <a-date-picker
            style="width:100%"
            type="date"
            placeholder="选择日期"
            v-decorator="[`startTime-${index}`, { initialValue: getMoment(item.startTime) }]"
            v-else
            @change="change2($event, index, 'startTime')"
          />
        </a-form-item>
      </a-col>
      <a-col :span="10">
        <a-form-item label="培训结束时间" :label-col="{ span: 9 }" :wrapper-col="{ span: 15 }">
          <span class="text_color" v-if="disable">{{ item.endTime }}</span>
          <a-date-picker
            style="width:100%"
            type="date"
            placeholder="选择日期"
            v-decorator="[`endTime-${index}`, { initialValue: getMoment(item.endTime) }]"
            v-else
            @change="change2($event, index, 'endTime')"
          />
        </a-form-item>
      </a-col>

      <a-col :span="10">
        <a-form-item label="培训成绩" :label-col="{ span: 9 }" :wrapper-col="{ span: 15 }">
          <span class="text_color" v-if="disable">{{ item.assessResult }}</span>
          <a-input
            v-decorator="[`assessResult-${index}`, { initialValue: item.assessResult }]"
            v-else
            @change="change($event, index, 'assessResult')"
          />
        </a-form-item>
      </a-col>

      <a-col :span="10">
        <a-form-item label="培训证书名称" :label-col="{ span: 9 }" :wrapper-col="{ span: 15 }">
          <span class="text_color" v-if="disable">{{ item.trainCerName }}</span>
          <a-input
            v-decorator="[`trainCerName-${index}`, { initialValue: item.trainCerName }]"
            v-else
            @change="change($event, index, 'trainCerName')"
          />
        </a-form-item>
      </a-col>
      <a-col :span="10">
        <a-form-item label="培训证书编号" :label-col="{ span: 9 }" :wrapper-col="{ span: 15 }">
          <span class="text_color" v-if="disable">{{ item.trainCerNumber }}</span>
          <a-input
            v-decorator="[`trainCerNumber-${index}`, { initialValue: item.trainCerNumber }]"
            v-else
            @change="change($event, index, 'trainCerNumber')"
          />
        </a-form-item>
      </a-col>
      <a-col :span="10">
        <a-form-item label="培训备注" :label-col="{ span: 9 }" :wrapper-col="{ span: 15 }">
          <span class="text_color" v-if="disable">{{ item.remake }}</span>
          <a-input v-decorator="[`remake-${index}`, { initialValue: item.remake }]" v-else @change="change($event, index, 'remake')" />
        </a-form-item>
      </a-col>
      <a-col :span="10">
        <a-form-item :label-col="{ span: 9 }" :wrapper-col="{ span: 15 }">
          <a-input v-decorator="[`id-${index}`, { initialValue: String(item.id) }]" type="hidden" />
        </a-form-item>
      </a-col>
      <a-col v-if="!disable" :span="17" class="mb24">
        <a-button type="dashed" style="width:100%;height:44px;" @click="delHandle(item)">删除</a-button>
      </a-col>
      <a-col v-if="index != empTrain.empTrainList.length - 1" :span="17" class="border-top mb12"></a-col>
    </div>
    <div v-if="!disable">
      <a-col :span="17" class="border-top"></a-col>
      <a-col :span="17" class="mt24 mb24">
        <a-button type="dashed" style="width:100%;height:44px;" @click="addHandle">新增</a-button>
      </a-col>
      <a-col :span="23">
        <div class="basicInfo-button-group">
          <a-button class="mr12" @click="cancel">取消</a-button>
          <a-button type="primary" @click="submit">确认</a-button>
        </div>
      </a-col>
    </div>
  </a-form>
</template>
<script>
import { updateEmpAllInfo } from "@/api/staff/staff";
export default {
  name: "TrainForm",
  props: {
    empTrain: {
      type: Object
    },
    disable: {}
  },
  data() {
    return {
      form: this.$form.createForm(this)
    };
  },
  watch: {
    empTrain() {
      if (this.empTrain.empTrainList == null || this.empTrain.empTrainList.length === 0) {
        this.$set(this.empTrain, "empTrainList", [{}]);
      }
    }
  },
  methods: {
    change(e, index, key) {
      this.$set(this.empTrain.empTrainList, index, {
        ...this.empTrain.empTrainList[index],
        [key]: e.target.value
      });
    },
    change1(e, index, key) {
      this.$set(this.empTrain.empTrainList, index, {
        ...this.empTrain.empTrainList[index],
        [key]: e
      });
    },
    change2(e, index, key) {
      this.$set(this.empTrain.empTrainList, index, {
        ...this.empTrain.empTrainList[index],
        [key]: this.getMoment(e)
      });
    },
    cancel() {
      this.$emit("cancelEdit");
      if (!this.empTrain.empTrainList || this.empTrain.empTrainList.length === 0) {
        this.empTrain.empTrainList.push({ guid: Math.random() * 16 });
      }
    },
    addHandle() {
      if (this.empTrain.empTrainList === null) {
        this.empTrain.empTrainList = [];
      }
      this.empTrain.empTrainList.push({ guid: Math.random() * 16 });
    },
    delHandle(item) {
      const index = this.empTrain.empTrainList.indexOf(item);

      if (index !== -1) {
        this.empTrain.empTrainList.splice(index, 1);
      }
    },
    getMoment(date) {
      return date ? this.moment(date) : null;
    },
    submit() {
      var that = this;
      this.form.validateFields((err, values) => {
        if (!err) {
          const value = this.beautyData(values);

          setTimeout(() => {
            updateEmpAllInfo({
              id: this.empTrain.id,
              phone: this.empTrain.phone,
              empTrainList: value
            }).then(res => {
              that.$notification["success"]({
                message: "success",
                description: "编辑成功"
              });

              that.$emit("cancelEdit");
            });
          }, 300);
        }
      });
    },
    beautyData(data) {
      var arr = [];
      this.empTrain.empTrainList.forEach((item, index) => {
        arr[index] = {
          trainCourse: data[`trainCourse-${index}`],
          trainCerName: data[`trainCerName-${index}`],
          trainOrgName: data[`trainOrgName-${index}`],
          assessResult: data[`assessResult-${index}`],
          startTime: this.getMoment(data[`startTime-${index}`]),
          endTime: this.getMoment(data[`endTime-${index}`]),
          trainCerNumber: data[`trainCerNumber-${index}`],
          remake: data[`remake-${index}`],
          id: data[`id-${index}`]
        };
      });
      return arr;
    }
  }
};
</script>

<style scoped>
.basicInfo-button-group {
  text-align: center;
  padding-bottom: 24px;
}
</style>
